﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAMailUtils.MailGun
{
    public class AddressInfo
    {
        public string display_name { get; set; }
        public string domain { get; set; }
        public string local_part { get; set; }
    }
    public class EmailValidateResult
    {
        public bool is_valid { get; set; }
        public AddressInfo parts { get; set; }
        public string address { get; set; }
        public string did_you_mean { get; set; }
    }
    public class EmailValidate
    {
        string ApiKey;
        public EmailValidate(string apiKey)
        {
            ApiKey = apiKey;
        }
        public EmailValidateResult GetValidate(string address)
        {
            EmailValidateResult result = new EmailValidateResult();
            try
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.mailgun.net/v2";
                client.Authenticator =
                        new HttpBasicAuthenticator("api",
                                                   ApiKey);
                RestRequest request = new RestRequest();
                request.Resource = "/address/validate";
                request.AddParameter("address", address);
                IRestResponse response = client.Execute(request);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<EmailValidateResult>(response.Content);
            }
            catch (Exception ex) { }
            return result;
        }
    }
}
